package com.touhou.youmu.config

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport


/**
 * 拦截器
 * 这段代码不需要详细知道什么意思，知道这个代码更改后，
 * 视频放到/D:/upload/后，从磁盘读取的视频数据scr将会变成file/###.mp4的格式。这样改了之后，网页的静态视频资源就需要放到指定的路径中了，比如：
 * <video src="file/01_Blender's Interface.mp4"  width="320" height="240" controls="controls"></video>
 */
@Configuration
class WebAppConfig: WebMvcConfigurationSupport() {
    // 从配置文件读取文件路径
    @Value(value = "\${my.filePath}")
    private var mfilePath: String? =""

    //静态资源处理
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/")

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")



        if (mfilePath == "" || mfilePath == "\${my.filePath}") {
            var filePath = WebAppConfig::class.java.classLoader.getResource("")!!.path
            if (filePath.indexOf(".jar") > 0) {
                filePath = filePath.substring(0, filePath.indexOf(".jar"))
            } else if (filePath.indexOf("classes") > 0) {
                filePath = "file:" + filePath.substring(0, filePath.indexOf("classes"))
            }
            filePath = filePath.substring(0, filePath.lastIndexOf("/")) + "/file/"
            mfilePath = filePath
        }
        registry!!.addResourceHandler("/file/**").addResourceLocations(mfilePath!!)
        // TODO Auto-generated method stub
        super.addResourceHandlers(registry)
    }
}